package com.mall.business.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.business.domain.TMemberLevel;
import com.mall.business.domain.TProductUserMoney;
import com.mall.business.service.*;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.StringUtils;
import io.jsonwebtoken.lang.Collections;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TProductMapper;
import com.mall.business.domain.TProduct;

/**
 * 商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-29
 */
@Service
public class TProductServiceImpl extends ServiceImpl<TProductMapper,TProduct>  implements ITProductService
{
    @Autowired
    private TProductMapper tProductMapper;

    @Autowired
    private ITProductUserMoneyService productUserMoneyService;

    @Autowired
    private ITMemberLevelService tMemberLevelServie;
    @Autowired
    private ITWalletService walletService;

    @Autowired
    private ITOrderService  orderService;




    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public TProduct selectTProductById(String id)
    {
        return tProductMapper.selectTProductById(id);
    }

    @Override
    public TProduct selectTProductByIdApp(String id,String userId)
    {
        TProduct tProduct = tProductMapper.selectTProductById(id);
        //
        BigDecimal vipPrice = this.getVipPrice(userId, id);
        tProduct.setVipPrice(vipPrice);
        return  tProduct;
    }

    /**
     * 查询商品信息列表
     *
     * @param tProduct 商品信息
     * @return 商品信息
     */
    @Override
    public List<TProduct> selectTProductList(TProduct tProduct)
    {
        return tProductMapper.selectTProductList(tProduct);
    }

    @Override
    public List<TProduct> selectTProductListApp(TProduct tProduct)
    {
        return tProductMapper.selectTProductList(tProduct);
    }

    /**
     * 新增商品信息
     *
     * @param tProduct 商品信息
     * @return 结果
     */
    @Override
    public int insertTProduct(TProduct tProduct)
    {
                tProduct.setCreateTime(DateUtils.getNowDate());
            return tProductMapper.insertTProduct(tProduct);
    }

    /**
     * 修改商品信息
     *
     * @param tProduct 商品信息
     * @return 结果
     */
    @Override
    public int updateTProduct(TProduct tProduct)
    {
                tProduct.setUpdateTime(DateUtils.getNowDate());
        return tProductMapper.updateTProduct(tProduct);
    }

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteTProductByIds(String[] ids)
    {
        return tProductMapper.deleteTProductByIds(ids);
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteTProductById(String id)
    {
        return tProductMapper.deleteTProductById(id);
    }

    /**
     *
     *
     * 获取vip 价格
     *
     */
    @Override
    public BigDecimal  getVipPrice(String userId,String productId){
        TProduct product = this.getById(productId);
        BigDecimal price = product.getYlPrice();
        if(StringUtils.isEmpty(userId)){
            return price;

        }
        // 一客一价
        TProductUserMoney  userMoney =new TProductUserMoney();
        userMoney.setUserId(userId);
        userMoney.setProductId(productId);
        List<TProductUserMoney> tProductUserMonies = productUserMoneyService.selectTProductUserMoneyList(userMoney);
        if(!Collections.isEmpty(tProductUserMonies)){
            TProductUserMoney userMoney1 = tProductUserMonies.get(0);
             price = userMoney1.getPrice();
        }else {
            // 根据用户等级获取折扣
            BigDecimal totalMoney = orderService.getOrderPayMoney(userId);
            TMemberLevel userMemberLevel = tMemberLevelServie.getUserMemberLevel(totalMoney);
            if(userMemberLevel!=null){
                BigDecimal rebate = userMemberLevel.getRebate();
                System.out.println(rebate+"折扣=======================");
                System.out.println(price+"价格=======================");
                price = price.multiply(rebate).setScale(2, RoundingMode.HALF_UP);
            }
        }
        return  price;
    }

    @Override
    public   int updateStocks(TProduct  product){
        return tProductMapper.updateStocks(product);
    }





}
